********************************************************************************

/*
Find the t-1 and parental deciles
*/

use shnro vuosi event_ie event_uie ptile_dspi_tmin1 ptile_prnt prnt_entr using "$data\01_main\folk_flown_merge_eventready.dta", clear
keep if event_ie==0
egen dile_dspi_own = cut(ptile_dspi_tmin1), at(1(10)101)
egen dile_dspi_prnt = cut(ptile_prnt), at(1(10)101)
foreach var of varlist dile_dspi_own dile_dspi_prnt {
	replace `var' = (`var'+9)/10
}
keep dile_dspi_own dile_dspi_prnt shnro prnt_entr
save "$data\99_temp\dileranks_temp.dta", replace

********************************************************************************

/*
New incorporated businesses that are founded by new entrepreneurs
*/

use "$data\01_main\folk_flown_merge_eventready_iespec.dta", clear
keep shnro vuosi syrtun
merge 1:1 shnro vuosi using "$data\01_main\folk_flown_merge_eventready.dta", keep(3) keepusing(event_ie)
drop _merge
keep if syrtun!=""
keep if event_ie==0
drop event_ie
duplicates drop syrtun vuosi, force
merge 1:1 shnro using "$data\99_temp\dileranks_temp.dta"
keep if _merge==3
drop _merge
save "$data\99_temp\newfirms_temp.dta", replace
erase "$data\99_temp\dileranks_temp.dta"

* Open FSS (financial statements panel)
use "D:\ready-made\FIRM_FSS\Tilinpaatospaneeli\tp8620.dta", clear
destring vuosi, replace
keep if syrtun!=""
sort vuosi
duplicates drop syrtun vuosi, force
drop if vuosi<1998
keep syrtun vuosi

* Merge the tags
merge 1:1 syrtun vuosi using "$data\99_temp\newfirms_temp.dta"
drop if _merge==2
gen newfirm=0
replace newfirm=1 if _merge==3
drop _merge
bysort syrtun: egen newfirmsometime = max(newfirm)
keep if newfirmsometime==1
drop newfirmsometime
keep syrtun vuosi newfirm dile_dspi_own dile_dspi_prnt prnt_entr
egen long id = group(syrtun)
order syrtun id
tsset id vuosi, yearly
tsfill, full
drop if vuosi==2020
duplicates drop syrtun vuosi, force
drop if syrtun==""
save "$data\99_temp\firmlifespan_temp.dta", replace

* Open bankruptcy data
use "D:\ready-made\FIRM_BANKR\konkurssit_012017_032020.dta", clear
keep KAPV1 LPPV1 syrtun
destring KAPV1, replace
destring LPPV1, replace
keep if syrtun!=""
gen vuosi=KAPV1
replace vuosi=LPPV1 if vuosi==.
drop if vuosi==.
keep syrtun vuosi
drop if vuosi==2020
save "$data\99_temp\bnkrpt1719.dta", replace
use "D:\ready-made\FIRM_BANKR\konkurssit_19852016.dta", clear
keep KAPV1 LPPV1 syrtun
destring KAPV1, replace
destring LPPV1, replace
keep if syrtun!=""
gen vuosi=KAPV1
replace vuosi=LPPV1 if vuosi==.
drop if vuosi==.
keep syrtun vuosi
drop if vuosi<1998
append using "$data\99_temp\bnkrpt1719.dta"
gen bnkrpt=1
bysort syrtun: gen nr = _n
keep if nr==1
drop nr
erase "$data\99_temp\bnkrpt1719.dta"
save "$data\99_temp\bnkrpt.dta", replace

* Merge the bankruptcy information
use "$data\99_temp\firmlifespan_temp.dta", clear
erase "$data\99_temp\firmlifespan_temp.dta"
merge 1:1 syrtun vuosi using "$data\99_temp\bnkrpt.dta"
drop if _merge==2
replace bnkrpt=0 if bnkrpt==.
drop _merge
erase "$data\99_temp\bnkrpt.dta"
xtset id vuosi, yearly
gen posturnover_2yr=0
replace posturnover_2yr=1 if F.newfirm==1
drop if posturnover_2yr==1
drop posturnover_2yr
bys syrtun: egen min_dile_dspi_own = min(dile_dspi_own)
bys syrtun: egen min_dile_dspi_prnt = min(dile_dspi_prnt)
bys syrtun: egen min_prnt_entr = min(prnt_entr)
drop dile_dspi_own dile_dspi_prnt prnt_entr
ren min_dile_dspi_own dile_dspi_own
ren min_dile_dspi_prnt dile_dspi_prnt
ren min_prnt_entr prnt_entr
bysort syrtun: gen age = _n

save "$data\01_main\firmlifespan_bnkrpt.dta", replace

********************************************************************************

/*
Repeat exit analysis for IEs by checking which firms file taxes
*/

* Open FSS
use "D:\ready-made\FIRM_FSS\Tilinpaatospaneeli\tp8620.dta", clear
destring vuosi, replace
keep if syrtun!=""
sort vuosi
duplicates drop syrtun vuosi, force
drop if vuosi<1998
keep syrtun vuosi jal

* Merge the tags
merge 1:1 syrtun vuosi using "$data\99_temp\newfirms_temp.dta"
erase "$data\99_temp\newfirms_temp.dta"
drop if _merge==2
gen newfirm=0
replace newfirm=1 if _merge==3
drop _merge
bysort syrtun: egen newfirmsometime = max(newfirm)
keep if newfirmsometime==1
keep syrtun vuosi newfirm dile_dspi_own dile_dspi_prnt jal newfirmsometime prnt_entr
egen long id = group(syrtun)
order syrtun id
bys id: gen count=_n
tsset id vuosi, yearly
tsfill, full
drop if vuosi==2020
duplicates drop id vuosi, force
tsset id vuosi
bys id: egen max=max(count)
gen posturnover_2yr=0
replace posturnover_2yr=1 if L.newfirmsometime==1
bys id: carryforward count, gen(count2)
gen exit=1 if syrtun=="" & posturnover_2yr==1 & max==count2
bys id: carryforward syrtun, gen(syrtun2)
drop syrtun
ren syrtun2 syrtun

drop if jal==. & exit==.

bys syrtun: egen min_dile_dspi_own = min(dile_dspi_own)
bys syrtun: egen min_dile_dspi_prnt = min(dile_dspi_prnt)
bys syrtun: egen min_prnt_entr = min(prnt_entr)
drop dile_dspi_own dile_dspi_prnt prnt_entr
ren min_dile_dspi_own dile_dspi_own
ren min_dile_dspi_prnt dile_dspi_prnt
ren min_prnt_entr prnt_entr
ren count2 age
replace age=age+1 if exit==1
replace exit=0 if exit==.

gen start_year=vuosi if newfirm==1
bys syrtun: egen maxsty=max(start_year)
gen theory_age=2019-maxsty
keep if theory_age>=10

save "$data\01_main\firmlifespan_taxfile.dta", replace
